<template>
  <div 
    :class="[
      'bx-switch',
      {
        'bx-switch--checked': isChecked,
        'bx-switch--disabled': disabled,
        [`bx-switch--${size}`]: size
      }
    ]"
    @click="handleClick"
  >
    <input
      ref="inputRef"
      v-model="switchValue"
      type="checkbox"
      :disabled="disabled"
      class="bx-switch__input"
      @change="handleChange"
    />
    <span class="bx-switch__core bx-glass">
      <span class="bx-switch__button">
        <BxIcon v-if="checkedIcon && isChecked" :name="checkedIcon" class="bx-switch__icon" />
        <BxIcon v-else-if="uncheckedIcon && !isChecked" :name="uncheckedIcon" class="bx-switch__icon" />
      </span>
    </span>
    <span v-if="checkedText || uncheckedText" class="bx-switch__text">
      {{ isChecked ? checkedText : uncheckedText }}
    </span>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import BxIcon from '../basic/BxIcon.vue'

export default {
  name: 'BxSwitch',
  components: {
    BxIcon
  },
  props: {
    modelValue: {
      type: [Boolean, String, Number],
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    size: {
      type: String,
      default: 'medium',
      validator: (value) => ['small', 'medium', 'large'].includes(value)
    },
    checkedValue: {
      type: [Boolean, String, Number],
      default: true
    },
    uncheckedValue: {
      type: [Boolean, String, Number],
      default: false
    },
    checkedText: {
      type: String,
      default: ''
    },
    uncheckedText: {
      type: String,
      default: ''
    },
    checkedIcon: {
      type: String,
      default: ''
    },
    uncheckedIcon: {
      type: String,
      default: ''
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:modelValue', 'change'],
  setup(props, { emit }) {
    const inputRef = ref(null)

    const isChecked = computed(() => {
      return props.modelValue === props.checkedValue
    })

    const switchValue = computed({
      get() {
        return isChecked.value
      },
      set(value) {
        const newValue = value ? props.checkedValue : props.uncheckedValue
        emit('update:modelValue', newValue)
        emit('change', newValue)
      }
    })

    const handleClick = () => {
      if (props.disabled || props.loading) return
      switchValue.value = !switchValue.value
    }

    const handleChange = (event) => {
      if (props.disabled || props.loading) return
      // 事件已经在 handleClick 中处理
    }

    return {
      inputRef,
      isChecked,
      switchValue,
      handleClick,
      handleChange
    }
  }
}
</script>

<style scoped>
.bx-switch {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  font-family: var(--bx-font-family);
  user-select: none;
  transition: all var(--bx-transition-medium);
}

.bx-switch--disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.bx-switch__input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  margin: 0;
}

.bx-switch__core {
  position: relative;
  display: inline-block;
  border: 1px solid var(--bx-glass-border);
  border-radius: 20px;
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  transition: all var(--bx-transition-medium);
}

.bx-switch--small .bx-switch__core {
  width: 32px;
  height: 18px;
}

.bx-switch--medium .bx-switch__core {
  width: 40px;
  height: 22px;
}

.bx-switch--large .bx-switch__core {
  width: 48px;
  height: 26px;
}

.bx-switch--checked .bx-switch__core {
  background: rgba(0, 122, 255, 0.25);
  border-color: rgba(0, 122, 255, 0.5);
}

.bx-switch__button {
  position: absolute;
  top: 2px;
  left: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bx-text-primary);
  border-radius: 50%;
  transition: all var(--bx-transition-medium);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.bx-switch--small .bx-switch__button {
  width: 14px;
  height: 14px;
}

.bx-switch--medium .bx-switch__button {
  width: 18px;
  height: 18px;
}

.bx-switch--large .bx-switch__button {
  width: 22px;
  height: 22px;
}

.bx-switch--small.bx-switch--checked .bx-switch__button {
  transform: translateX(14px);
}

.bx-switch--medium.bx-switch--checked .bx-switch__button {
  transform: translateX(18px);
}

.bx-switch--large.bx-switch--checked .bx-switch__button {
  transform: translateX(22px);
}

.bx-switch__icon {
  font-size: 10px;
  color: var(--bx-glass-bg);
}

.bx-switch--medium .bx-switch__icon {
  font-size: 12px;
}

.bx-switch--large .bx-switch__icon {
  font-size: 14px;
}

.bx-switch__text {
  margin-left: 8px;
  color: var(--bx-text-primary);
  font-size: 14px;
  line-height: 1.5;
}

.bx-switch--disabled .bx-switch__text {
  color: var(--bx-text-tertiary);
}

/* 焦点样式 */
.bx-switch__input:focus + .bx-switch__core {
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.1);
}

/* 悬停效果 */
.bx-switch:not(.bx-switch--disabled):hover .bx-switch__core {
  border-color: var(--bx-glass-border-hover);
  background: var(--bx-glass-bg-hover);
}

.bx-switch--checked:not(.bx-switch--disabled):hover .bx-switch__core {
  background: rgba(0, 122, 255, 0.35);
  border-color: rgba(0, 122, 255, 0.6);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-switch__text {
    font-size: 13px;
    margin-left: 6px;
  }
}
</style>

